<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="选择品种" prop="plantVarietyId">
                <el-select v-model="ruleForm.plantVarietyId" class="w100p" filterable placeholder="请选择养殖品种"
                    @change="handleChangePlantVariety">
                    <el-option v-for="item, index in varietyList" :key="index" :label="item.plantVarietyName"
                        :value="item.plantVarietyId"></el-option>
                </el-select>
            </el-form-item>
            <el-row>
                <el-col :span="20">
                    <el-form-item label="密度" prop="breedingDensity">
                        <el-input type="number" v-model="ruleForm.breedingDensity" class="w100p"
                            placeholder="请输入密度值"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item prop="densityUnit" class="noLabel">
                        <el-select v-model="ruleForm.densityUnit" class="w100p" filterable placeholder="请选择单位">
                            <el-option v-for="item, index in getUnit()" :key="index" :label="item.name"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import densityManagement from '@/api/basicInformation/densityManagement.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        varietyList: {
            type: Array,
            default: () => {
                return []
            }
        },
        unitList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value <= 999999) {
                callback()
            } else {
                callback(new Error('请输入大于0小于999999的数值！'))
            }
        }
        return {
            addEditName: '添加',
            addEditApi: densityManagement.add,
            ruleForm: {},
            rules: {
                plantVarietyId: [
                    { required: true, message: '请选择品种', trigger: 'change' }
                ],
                breedingDensity: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                densityUnit: [
                    { required: true, message: '请选择单位', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        handleChangePlantVariety() {
            let plantVarietyName = this.$public.foundObject(this.varietyList, 'plantVarietyId', this.ruleForm.plantVarietyId).plantVarietyName || '';
            this.$set(this.ruleForm, 'plantVarietyName', plantVarietyName)
            console.log(plantVarietyName)
        },
        getUnit() {
            let unitTypeObj = this.$store.state.initData.enum.densityUnitType;
            let list = []
            for (let key in unitTypeObj) {
                list.push({ id: key, name: unitTypeObj[key] })
            }
            return list
        },
        initData() {
            if (this.addEditFlag == 'add') {
                this.ruleForm = {};
                this.resetForm();
                this.addEditApi = densityManagement.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data };
                this.addEditApi = densityManagement.edit;
                this.addEditName = '编辑';
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
::v-deep .noLabel {
    .el-form-item__content {
        margin: 0 !important;
    }
}
</style>
  